package com.jaydenxiao.androidfire.ui.main.fragment;

import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.aspsine.irecyclerview.OnLoadMoreListener;
import com.aspsine.irecyclerview.OnRefreshListener;
import com.aspsine.irecyclerview.universaladapter.ViewHolderHelper;
import com.aspsine.irecyclerview.universaladapter.recyclerview.CommonRecycleViewAdapter;
import com.aspsine.irecyclerview.widget.LoadMoreFooterView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jaydenxiao.androidfire.R;
import com.jaydenxiao.androidfire.bean.PhotoGirl;
import com.jaydenxiao.androidfire.ui.news.activity.PhotosDetailActivity;
import com.jaydenxiao.androidfire.ui.news.contract.PhotoListContract;
import com.jaydenxiao.androidfire.ui.news.model.PhotosListModel;
import com.jaydenxiao.androidfire.ui.news.presenter.PhotosListPresenter;
import com.jaydenxiao.common.base.BaseFragment;
import com.jaydenxiao.common.commonwidget.LoadingTip;
import com.jaydenxiao.common.commonwidget.NormalTitleBar;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.Bind;

import static java.lang.System.currentTimeMillis;

/**
 * des:图片首页
 */
public class PhotosMainFragment extends BaseFragment<PhotosListPresenter, PhotosListModel> implements PhotoListContract.View, OnRefreshListener, OnLoadMoreListener {
    @Bind(R.id.ntb)
    NormalTitleBar ntb;
    @Bind(R.id.irc)
    IRecyclerView irc;
    @Bind(R.id.loadedTip)
    LoadingTip loadedTip;
    @Bind(R.id.fab)
    FloatingActionButton fab;
    private CommonRecycleViewAdapter<PhotoGirl> adapter;
    private static int SIZE = 20;
    private int mStartPage = 1;
    String TAG = "PhotosMainFragment";
    public int i = 0;
    public static   List<Integer> phlist = new ArrayList<>();

    @Override
    protected int getLayoutResource() {
        return R.layout.act_photos_list;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    public void initView() {
        initPhoto();
        ntb.setTvLeftVisiable(false);
        ntb.setTitleText(getString(R.string.girl_title));
        adapter = new CommonRecycleViewAdapter<PhotoGirl>(getContext(), R.layout.item_photo) {
            @Override
            public void convert(ViewHolderHelper helper, final PhotoGirl Myphoto) {
                ImageView imageView = helper.getView(R.id.iv_photo);
                Glide.with(mContext).load(Myphoto.getUrl())
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                        .placeholder(R.drawable.ic_image_loading)
                        .error(phlist.get(i))
                        .centerCrop().override(1090, 1090 * 3 / 4)
                        .crossFade().into(imageView);

                if (i == 35) {
                    i = 0;
                } else {
                    Log.e(TAG, "convert: " + i++ );
                }

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PhotosDetailActivity.startAction(mContext, Myphoto.getUrl(),phlist.get(--i));
                        Log.e(TAG, "onClick: " +   --i);
                    }
                });
            }
        };
        irc.setAdapter(adapter);
        irc.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        irc.setOnLoadMoreListener(this);
        irc.setOnRefreshListener(this);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                irc.smoothScrollToPosition(0);
            }
        });
        mPresenter.getPhotosListDataRequest(SIZE, mStartPage);
    }

    private void initPhoto() {
        phlist.add(R.drawable.ph1);
        phlist.add(R.drawable.ph2);
        phlist.add(R.drawable.ph3);
        phlist.add(R.drawable.ph4);
        phlist.add(R.drawable.ph5);
        phlist.add(R.drawable.ph6);
        phlist.add(R.drawable.ph7);
        phlist.add(R.drawable.ph8);
        phlist.add(R.drawable.ph9);
        phlist.add(R.drawable.ph10);
        phlist.add(R.drawable.ph11);
        phlist.add(R.drawable.ph12);
        phlist.add(R.drawable.ph13);
        phlist.add(R.drawable.ph14);
        phlist.add(R.drawable.ph15);
        phlist.add(R.drawable.ph16);
        phlist.add(R.drawable.ph17);
        phlist.add(R.drawable.ph18);
        phlist.add(R.drawable.ph19);
        phlist.add(R.drawable.ph20);
        phlist.add(R.drawable.ph21);
        phlist.add(R.drawable.ph22);
        phlist.add(R.drawable.ph23);
        phlist.add(R.drawable.ph24);
        phlist.add(R.drawable.ph25);
        phlist.add(R.drawable.ph26);
        phlist.add(R.drawable.ph27);
        phlist.add(R.drawable.ph28);
        phlist.add(R.drawable.ph29);
        phlist.add(R.drawable.ph30);
        phlist.add(R.drawable.ph31);
        phlist.add(R.drawable.ph32);
        phlist.add(R.drawable.ph33);
        phlist.add(R.drawable.ph34);
        phlist.add(R.drawable.ph35);
        phlist.add(R.drawable.ph36);
    }

    @Override
    public void returnPhotosListData(List<PhotoGirl> photoGirls) {
        if (photoGirls != null) {
            mStartPage += 1;
            if (adapter.getPageBean().isRefresh()) {
                irc.setRefreshing(false);
                adapter.replaceAll(photoGirls);
            } else {
                if (photoGirls.size() > 0) {
                    irc.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
                    adapter.addAll(photoGirls);
                } else {
                    irc.setLoadMoreStatus(LoadMoreFooterView.Status.THE_END);
                }
            }
        }
    }

    @Override
    public void showLoading(String title) {
        if (adapter.getPageBean().isRefresh())
            loadedTip.setLoadingTip(LoadingTip.LoadStatus.loading);
    }

    @Override
    public void stopLoading() {
        loadedTip.setLoadingTip(LoadingTip.LoadStatus.finish);
    }

    @Override
    public void showErrorTip(String msg) {
        if (adapter.getPageBean().isRefresh()) {
            loadedTip.setLoadingTip(LoadingTip.LoadStatus.error);
            loadedTip.setTips(msg);
            irc.setRefreshing(false);
        } else {
            irc.setLoadMoreStatus(LoadMoreFooterView.Status.ERROR);
        }
    }

    @Override
    public void onRefresh() {
        adapter.getPageBean().setRefresh(true);
        mStartPage = 0;
        //发起请求
        irc.setRefreshing(true);
        mPresenter.getPhotosListDataRequest(SIZE, mStartPage);
    }

    @Override
    public void onLoadMore(View loadMoreView) {
        adapter.getPageBean().setRefresh(false);
        //发起请求
        irc.setLoadMoreStatus(LoadMoreFooterView.Status.LOADING);
        mPresenter.getPhotosListDataRequest(SIZE, mStartPage);
    }

}
